/*
 * Copyright 2012-2014 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jqd.examples.jpa2.lazy;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;

/**
 * TODO
 * 
 * @author liaozhicheng.cn@gmail.com
 * @date 2013-3-4
 * @since 1.0
 */
@Entity
public class Class implements Serializable {
	
	private static final long serialVersionUID = -1080077069102215432L;
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;
	
	private String className;
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "class_teacher", 
		joinColumns = @JoinColumn(name = "class_id"), 
		inverseJoinColumns = @JoinColumn(name = "teacher_id"))
	private Set<Teacher> teachers = new HashSet<Teacher>();
	
	@OneToMany(mappedBy = "classes", fetch = FetchType.LAZY)
	private Set<Student> students = new HashSet<Student>();
	
	
	public void addTeacher(Teacher teacher) {
		teachers.add(teacher);
	}
	
	
	public Integer getId() {
		return id;
	}
	
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	
	public String getClassName() {
		return className;
	}
	
	
	public void setClassName(String className) {
		this.className = className;
	}
	
	
	public Set<Teacher> getTeachers() {
		return teachers;
	}
	
	
	public void setTeachers(Set<Teacher> teachers) {
		this.teachers = teachers;
	}
	
	
	public Set<Student> getStudents() {
		return students;
	}
	
	
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Class other = (Class) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		}
		else if (!id.equals(other.id))
			return false;
		return true;
	}
	
}
